package com.zhang.servicedriveruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.internalcommon.constant.CommonStatusEnum;
import com.zhang.internalcommon.constant.DriverCarConstant;
import com.zhang.internalcommon.dto.DriverCarBindingRelationship;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.zhang.servicedriveruser.service.IDriverCarBindingRelationshipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张开琦
 * @since 2025-04-12
 */
@Service
public class DriverCarBindingRelationshipServiceImpl extends ServiceImpl<DriverCarBindingRelationshipMapper, DriverCarBindingRelationship> implements IDriverCarBindingRelationshipService {
	@Autowired
	private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

	@Override
	public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship){
		// 判断如果参数中的司机和车辆已经做过绑定，则不允许进行绑定
		QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
		queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
		queryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);

		Integer count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
		if (count > 0) {
			return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue());
		}

		// 司机被绑定了
		queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
		queryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);

		count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
		if (count > 0) {
			return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_BIND_EXISTS.getValue());
		}

		// 车辆被绑定了
		queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
		queryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);

		count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
		if (count > 0) {
			return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXISTS.getCode(),CommonStatusEnum.CAR_BIND_EXISTS.getValue());
		}

		LocalDateTime now = LocalDateTime.now();
		driverCarBindingRelationship.setBindingTime(now);
		driverCarBindingRelationship.setBindState(DriverCarConstant.DRIVER_CAR_BIND);
		driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
		return ResponseResult.success();
	}

	@Override
	public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship) {
		LocalDateTime now = LocalDateTime.now();

		HashMap<String, Object> map = new HashMap<>();
		map.put("driver_id", driverCarBindingRelationship.getDriverId());
		map.put("car_id", driverCarBindingRelationship.getCarId());
		map.put("bind_state", DriverCarConstant.DRIVER_CAR_BIND);

		List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingRelationshipMapper.selectByMap(map);
		if (driverCarBindingRelationships.isEmpty()) {
			return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue());
		}
		DriverCarBindingRelationship relationship = driverCarBindingRelationships.get(0);
		relationship.setBindState(DriverCarConstant.DRIVER_CAR_UNBIND);
		relationship.setUnBindingTime(now);

		driverCarBindingRelationshipMapper.updateById(relationship);
		return ResponseResult.success();

	}
}
